package com.master.rabbitmq;

import com.master.redis.RedisServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


//生产者
@Service
public class MQSender {

    @Autowired
    AmqpTemplate amqpTemplate;

    private static Logger logger = LoggerFactory.getLogger(MQSender.class);

//    //生产者将任务放入消息队列中
//    public void send(Object message){
//        String msg = RedisServer.beanToString(message);
//        logger.info("send message:"+msg);
//        //指定发送到哪个MQ
//        amqpTemplate.convertAndSend(MQConfig.QUEUE,msg);
//    }
//
//    public void sendTopic(Object message){
//        String msg = RedisServer.beanToString(message);
//        logger.info("send message:"+msg);
//        //指定发送到哪个MQ
//        amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE,MQConfig.ROUTING_KEY1,msg+"1");
//        amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE,MQConfig.ROUTING_KEY2,msg+"2");
//    }
//
//    public void sendFanout(Object message){
//        String msg = RedisServer.beanToString(message);
//        logger.info("send message:"+msg);
//        amqpTemplate.convertAndSend(MQConfig.FANOUT_EXCHANGE,"",msg);
//    }
//
//    public void sendHeader(Object message){
//        String msg = RedisServer.beanToString(message);
//        logger.info("send message:"+msg);
//        MessageProperties properties = new MessageProperties();
//        properties.setHeader("header1","value1");
//        properties.setHeader("header2","value2");
//        Message obj = new Message(msg.getBytes(),properties);
//        amqpTemplate.convertAndSend(MQConfig.HEADERS_EXCHANGE,"",obj);
//    }

    //将封装好的用户秒杀信息发送到消息队列中
    public void sendMiaoshaMessage(MiaoshaMessage message) {
        String msg = RedisServer.beanToString(message);
        logger.info("send message:"+msg);
        //指定发送到哪个MQ
         amqpTemplate.convertAndSend(MQConfig.MIAOSHA_QUEUE,msg);
    }
}
